package com.bluetrum.abmate.multipoint;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bluetrum.abmate.R;
import com.bluetrum.abmate.databinding.EndpointItemBinding;
import com.bluetrum.devicemanager.models.Multipoint;
import com.bluetrum.devicemanager.models.SinglePoint;

import java.util.ArrayList;
import java.util.List;

public class EndpointAdapter extends RecyclerView.Adapter<EndpointAdapter.ViewHolder> {

    private String localBluetoothAddress;
    private String localBluetoothName;

    private final List<SinglePoint> endpoints = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(@NonNull final OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public void setLocalBluetoothAddress(@NonNull String localBluetoothAddress) {
        this.localBluetoothAddress = localBluetoothAddress;
    }

    public void setLocalBluetoothName(@NonNull String localBluetoothName) {
        this.localBluetoothName = localBluetoothName;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
        EndpointItemBinding binding = EndpointItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final SinglePoint endpoint = endpoints.get(position);
        final String name = endpoint.getBluetoothName();

        if (!TextUtils.isEmpty(name)) {
            holder.deviceName.setText(name);
        } else {
            holder.deviceName.setText(R.string.unknown_name);
        }

        if (endpoint.getAddress().equalsIgnoreCase(localBluetoothAddress) &&
                endpoint.getBluetoothName().equalsIgnoreCase(localBluetoothName)) {
            holder.state.setText(R.string.function_multipoint_this_phone);
        } else {
            if (endpoint.isConnected()) {
                holder.state.setText(R.string.function_multipoint_connected);
            } else {
                holder.state.setText(R.string.function_multipoint_disconnected);
            }
        }
    }

    public synchronized void updateList(Multipoint multipoint) {
        List<SinglePoint> endpoints = multipoint.getEndpoints();
        this.endpoints.clear();
        this.endpoints.addAll(endpoints);
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(final int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return endpoints.size();
    }

    public boolean isEmpty() {
        return getItemCount() == 0;
    }

    @FunctionalInterface
    public interface OnItemClickListener {
        void onItemClick(final View view, final SinglePoint endpoint);
    }

    final class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView deviceName;
        private final TextView state;

        private ViewHolder(final EndpointItemBinding binding) {
            super(binding.getRoot());
            deviceName = binding.deviceName;
            state = binding.stateTextView;

            binding.actionButton.setOnClickListener(view -> {
                if (mOnItemClickListener != null) {
                    int pos = getBindingAdapterPosition();
                    if(pos > -1) {
                        mOnItemClickListener.onItemClick(view, endpoints.get(pos));
                    }
                }
            });
        }
    }

}
